# Contributor: Leo <thinkabit.ukim@gmail.com>
maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=mdcat
pkgver=2.5.0
pkgrel=0
pkgdesc="Cat for markdown"
url="https://github.com/swsnr/mdcat"
arch="all"
license="Apache-2.0"
makedepends="
	asciidoctor
	cargo
	cargo-auditable
	openssl-dev
	shared-mime-info
	"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	$pkgname-fish-completion
	"
source="https://github.com/swsnr/mdcat/archive/refs/tags/mdcat-$pkgver.tar.gz"
builddir="$srcdir/mdcat-mdcat-$pkgver"

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --release --frozen

	# Create a temporary symlink to generate different completion files below
	ln -sf mdcat target/release/mdless
	for binary in mdcat mdless; do
		for shell in bash fish zsh; do
			./target/release/$binary --completions $shell > $binary.$shell
		done
	done

	asciidoctor -b manpage -a reproducible -o mdcat.1 mdcat.1.adoc
}

check() {
	cargo test --frozen
}

package() {
	install -Dm755 target/release/mdcat -t "$pkgdir"/usr/bin/
	ln -s mdcat "$pkgdir"/usr/bin/mdless

	# Install the generated shell completion files
	for binary in mdcat mdless; do
		install -Dm644 $binary.bash \
			"$pkgdir"/usr/share/bash-completion/completions/$binary
		install -Dm644 $binary.fish \
			"$pkgdir"/usr/share/fish/vendor_completions.d/$binary.fish
		install -Dm644 $binary.zsh \
			"$pkgdir"/usr/share/zsh/site-functions/_$binary
	done

	# Install the generated man-page
	install -Dm644 "$builddir"/mdcat.1 -t "$pkgdir"/usr/share/man/man1/
}

sha512sums="
097739a642a45360e7e42effa4b963e189cfa48eac0f98150f3851e82af61b3e8b45fbb277cf63bc8e21b993f159bd207c8b9d819abe1e0ec81a71b9f7709743  mdcat-2.5.0.tar.gz
"
